package com.lz.netty.consumer.common.holder;

import cn.hutool.core.util.StrUtil;

import java.util.Map;
import java.util.WeakHashMap;

public class ContextHolder {

    private static InheritableThreadLocal<String> userHolder = new InheritableThreadLocal<>();

    private static InheritableThreadLocal<Map> paramHolder = new InheritableThreadLocal<>();

    private static InheritableThreadLocal<Map<String,Boolean>> paramPlHolder = new InheritableThreadLocal<>();

    public static void setUserId(String uId) {
        userHolder.set(uId);
    }

    public static String getUserId() {
        String uId = userHolder.get();
        if (StrUtil.isBlank(uId)){
            return "system";
        }
        return uId;
    }

    public static Map getParam() {
        if (paramHolder.get() == null) {
            paramHolder.set(new WeakHashMap());
        }
        return paramHolder.get();
    }
    public static Map<String,Boolean> getParamPl() {
        if (paramPlHolder.get() == null) {
            paramPlHolder.set(new WeakHashMap());
        }
        return paramPlHolder.get();
    }

    public static void reset() {
        userHolder.remove();
        paramHolder.remove();
        paramPlHolder.remove();
    }
}
